//
//  RONetworkMngTool.swift
//  NeusoftSchool
//
//  Created by 李狗蛋 on 2017/3/22.
//  Copyright © 2017年 李狗蛋. All rights reserved.
//

import UIKit
//全局变量
var userId : NSString!

let  single = RONetworkMngTool()

class RONetworkMngTool: NSObject {
    
    class func sharedNetwodkMngTool() -> RONetworkMngTool {
        
        return single
    }
    
    func RONetwork_Login(_ parameters: NSDictionary, view: UIView ,block: @escaping (_ flag: NSString) -> Void ) {
        
        MBProgressHUD.showAdded(to: view, animated: true)
        let afManger = AFHTTPRequestOperationManager()
        
        afManger.get(RO_Logon, parameters: parameters, success: {(operation: AFHTTPRequestOperation?, responseObject:Any?) in
            //print(responseObject)
            MBProgressHUD.hide(for: view, animated: true)
            //当成功登陆后保存用户名和密码
            let logonReturnModel = ROLoginReturnModel.logonReeturnModelWithDict(responseObject as! NSDictionary)
            if logonReturnModel.userid != "0" {
                userId = logonReturnModel.userid
                //print("登陆成功")
                block("1")
            }else{
                MBProgressHUD.showDelayHUDToView(view, message: logonReturnModel.msg as String)
                //print("登陆失败")
                block("0")
            }
        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) in
            MBProgressHUD.hide(for: view, animated: true)
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败")
            block("0")
        })
    }
    
    func RONetwork_Register(_ parameters: NSDictionary, view: UIView ,block: @escaping (_ flag: NSString) -> Void ) {
        
        MBProgressHUD.showAdded(to: view, animated: true)
        let afManger = AFHTTPRequestOperationManager()
        
        afManger.get(RO_Gegister, parameters: parameters, success: {(operation: AFHTTPRequestOperation?, responseObject:Any?) in
            //print(responseObject)
            MBProgressHUD.hide(for: view, animated: true)
            //当成功登陆后保存用户名和密码
            let registerReturnModel = RORegisterReturnModel.registerReeturnModelWithDict(responseObject as! NSDictionary)
            if registerReturnModel.success != "0" {
                //print("登陆成功")
                block("1")
            }else{
                MBProgressHUD.showDelayHUDToView(view, message: registerReturnModel.msg as String)
                //print("登陆失败")
                block("0")
            }
        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) in
            MBProgressHUD.hide(for: view, animated: true)
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败")
            block("0")
        })
    }
    
    //获取店铺列表
    func RONetwork_GetShopListView(_ view: UIView ,block: @escaping (_ shopList: NSArray) -> Void ) {
        
        MBProgressHUD.showAdded(to: view, animated: true)
        let afManger = AFHTTPRequestOperationManager()
        
        afManger.get(RO_GetShopList, parameters: nil, success: {(operation: AFHTTPRequestOperation?, responseObject:Any?) in
            
            //print(responseObject)
            
            MBProgressHUD.hide(for: view, animated: true)
            
            let shopArray = responseObject as! NSArray
            
            let shopMutableArray: NSMutableArray = NSMutableArray()
            
            for ashopDict in shopArray {
                let shopDict = ashopDict as! NSDictionary
                let shop: ROShopModel = ROShopModel.shopModelWithDict(shopDict)
                shopMutableArray.add(shop)
            }
            block(shopMutableArray)
            
        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) in
            MBProgressHUD.hide(for: view, animated: true)
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败")
        })
    }
    
    //获取当前店铺的所有菜单信息
    func RONetwork_GetFoodListParameters(_ parameters: NSDictionary, view: UIView ,block: @escaping (_ foodList: NSArray) -> Void ) -> Void {
        
        MBProgressHUD.showAdded(to: view, animated: true)
        let afManger = AFHTTPRequestOperationManager()
        print(parameters)
        afManger.post(RO_GetFoodList, parameters: parameters, success: {(operation: AFHTTPRequestOperation?, responseObject:Any?) -> Void in
            
            //print(responseObject)
            
            MBProgressHUD.hide(for: view, animated: true)
            
            let foodArray = responseObject as! NSArray
            
            let foodMutableArray: NSMutableArray = NSMutableArray()
            
            for afoodDict in foodArray {
                let foodDict = afoodDict as! NSDictionary
                let food: ROFoodModel = ROFoodModel.foodModelWithDict(foodDict)
                foodMutableArray.add(food)
            }
            block(foodMutableArray);
            
            }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
                MBProgressHUD.hide(for: view, animated: true)
                MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败")
            })
        }
    //获取当前菜谱的店铺信息
    func RONetwork_GetShopbyFood(_ parameters: NSDictionary, view: UIView ,block: @escaping (_ flag: NSString) -> Void ) -> Void {
        
        //MBProgressHUD.showAdded(to: view, animated: true)
        let afManger = AFHTTPRequestOperationManager()
        //print(parameters)
        afManger.post(RO_GetShopInformation, parameters: parameters, success: {(operation: AFHTTPRequestOperation?, responseObject:Any?) -> Void in
            
            //print(responseObject!)
            
         //   MBProgressHUD.hide(for: view, animated: true)
            
            //let shopNSMutableArray : NSMutableArray = NSMutableArray()
            //let shopDict : NSDictionary = responseObject as! NSDictionary
            //phonenum = shop.value(forKey: "phonenum") as! NSString
//            let shop : NSDictionary = ["address" : shopDict.value(forKey: "address")!,
//                                       "comment" : shopDict.value(forKey: "comment")!,
//                                       "intro" : shopDict.value(forKey: "intro")!,
//                                       "level" : shopDict.value(forKey: "level")!,
//                                       "phonenum" : shopDict.value(forKey: "phonenum")!,
//                                       "pic" : shopDict.value(forKey: "pic")!,
//                                       "shop_id" : shopDict.value(forKey: "shop_id")!,
//                                       "shopname" : shopDict.value(forKey: "shopname")!]
            //let shopa : ROShopbyFodd = ROShopbyFodd.shopbyFoodModelWithDict(shopDict)
            let res = responseObject as! NSDictionary
            let resp = res.object(forKey: "phonenum") as! NSString
            
            block(resp)
//            
        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
            //MBProgressHUD.hide(for: view, animated: true)
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败")
        })
    }
    //判断是否收藏
    func RONetwork_isCollect(_ parameters: NSDictionary, view: UIView ,block: @escaping (_ flag: NSString) -> Void) -> Void {
        
        let afManger = AFHTTPRequestOperationManager()
        
        afManger.post(RO_IsCollected, parameters: parameters, success: {(operation: AFHTTPRequestOperation?, responseObject:Any?) -> Void in
            
            let res = responseObject as! NSDictionary
            let collected = res.object(forKey: "collected") as! NSString
            if collected == "1" {
                block("1")
            }else{
                block("0")
            }
            }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
                //失败结束转
                MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败")
            })
        }
    
    //收藏／取消收藏店铺
    func RONetwork_collectShopWithParameters(_ parameters: NSDictionary, view: UIView ,block: @escaping (_ flag: NSString) -> Void) -> Void {
        
        let afManger = AFHTTPRequestOperationManager()
        
        afManger.post(RO_CollectShop, parameters: parameters, success: {(operation: AFHTTPRequestOperation?, responseObject:Any?) -> Void in
            
            let res = responseObject as! NSDictionary
            let collected = res.object(forKey: "success") as! NSString
            if collected == "1" {
                block("1")
            }else{
                block("0")
            }
            }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
                //失败结束转
                MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败")
            })
        }
    
    //收藏／取消收藏菜谱
    func RONetwork_collectFoodWithParameters(_ parameters: NSDictionary, view: UIView ,block: @escaping (_ flag: NSString) -> Void) -> Void {
        
        let afManger = AFHTTPRequestOperationManager()
        
        afManger.post(RO_CollectFood, parameters: parameters, success: {(operation: AFHTTPRequestOperation?, responseObject:Any?) -> Void in
            
            let res = responseObject as! NSDictionary
            let collected = res.object(forKey: "success") as! NSString
            if collected == "1" {
                block("1")
            }else{
                block("0")
            }
        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
            //失败结束转
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败")
        })
    }
    
    //插入下订单
    func RONetwork_insertOrderWithParameters(_ parameters: NSDictionary, view: UIView ,block: @escaping (_ flag: NSString) -> Void) -> Void {
        
        let afManger = AFHTTPRequestOperationManager()
        
        afManger.post(RO_InsertOrder, parameters: parameters, success: {(operation: AFHTTPRequestOperation?, responseObject:Any?) -> Void in
            
            let res = responseObject as! NSDictionary
            let collected = res.object(forKey: "success") as! NSString
            if collected == "1" {
                block("1")
            }else{
                block("0")
            }
        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
            //失败结束转
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败")
        })
    }
    
    //获取当前菜谱的所有评价列表接口
    func RONetwork_GetFoodCommentListParameters(_ parameters: NSDictionary, view: UIView ,block: @escaping (_ courseList: NSArray) -> Void ) -> Void {
        
        MBProgressHUD.showAdded(to: view, animated: true)
        let afManger = AFHTTPRequestOperationManager()
        print(parameters)
        afManger.post(RO_FoodCommentList, parameters: parameters, success: {(operation: AFHTTPRequestOperation?, responseObject:Any?) -> Void in
            
            print(responseObject!)
            
            MBProgressHUD.hide(for: view, animated: true)
            
            let commentArray = responseObject as! NSArray
            
            let commentMutableArray: NSMutableArray = NSMutableArray()
            
            for acommentDict in commentArray {
                let commentDict = acommentDict as! NSDictionary
                let comment: ROCommentModel = ROCommentModel.commentModelWithDict(commentDict)
                commentMutableArray.add(comment)
            }
            block(commentMutableArray)
            
        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
            MBProgressHUD.hide(for: view, animated: true)
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败")
        })
    }
    
    //获取当前用户的所有收藏信息接口
    func RONetwork_GetCollectionListParameters(_ parameters: NSDictionary,  view: UIView, block: @escaping (_ courseList: NSArray) -> Void) -> Void {
        //在开始网络加载前任务指示器开始转
        //MBProgressHUD.showAdded(to: view, animated: true)
        let manager = AFHTTPRequestOperationManager()
        print(parameters)
        manager.post(RO_CollectList, parameters: parameters, success: { (operation:AFHTTPRequestOperation?, responseObject: Any?) -> Void in
            //MBProgressHUD.hide(for: view, animated: true)
            //将返回的字典转换为模型对象
            print("所有收藏信息列表")
            print(responseObject)
            print("所有收藏信息列表")
            let collectionListArray = responseObject as! NSArray
            let collectionListMutableArray: NSMutableArray = NSMutableArray()
            
            for aCollectionDict in collectionListArray {
                let collectionDict = aCollectionDict as! NSDictionary
                let collection: ROCollectModel = ROCollectModel.collectionModelWithDict(collectionDict)
                collectionListMutableArray.add(collection)
            }
            block(collectionListMutableArray);
        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
            //失败结束转
            MBProgressHUD.hide(for: view, animated: true)
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败！")
        })
    }
    
    //根据foodid获取food所有信息
    func RONetwork_getFoodInformationWithParameters(_ parameters: NSDictionary,view: UIView, block: @escaping (_ food: ROFoodModel) -> Void) -> Void{
        let manager = AFHTTPRequestOperationManager()
        print(parameters)
        manager.post("http://172.24.10.130:8080/foodService/getFoodById.do", parameters: parameters, success: { (operation:AFHTTPRequestOperation?, responseObject: Any?) -> Void in
            let res = responseObject as! NSDictionary
            let aFood = ROFoodModel.foodModelWithDict(res) as ROFoodModel
            
            block(aFood)
            
        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
            //失败结束转
            MBProgressHUD.showDelayHUDToView(view, message: "获取菜谱信息失败！")
        })
    }
    
    //搜索
    func RONetwork_searchFoodListParameters(_ parameters: NSDictionary,  view: UIView, block: @escaping (_ courseList: NSArray) -> Void) -> Void {
        //在开始网络加载前任务指示器开始转
        MBProgressHUD.showAdded(to: view, animated: true)
        let manager = AFHTTPRequestOperationManager()
        //print(parameters)
        manager.post("http://172.24.10.130:8080/foodService/getFoodBySearch.do", parameters: parameters, success: { (operation:AFHTTPRequestOperation?, responseObject: Any?) -> Void in
            MBProgressHUD.hide(for: view, animated: true)
            //将返回的字典转换为模型对象
            //            print("菜单列表")
            //            print(responseObject)
            //            print("菜单列表")
            let foodArray = responseObject as! NSArray
            let foodMutableArray: NSMutableArray = NSMutableArray()
            
            for afoodDict in foodArray {
                let foodDict = afoodDict as! NSDictionary
                let food: ROFoodModel = ROFoodModel.foodModelWithDict(foodDict)
                foodMutableArray.add(food)
            }
            block(foodMutableArray);
        }, failure: { (operation: AFHTTPRequestOperation?, error: Error?) -> Void in
            //失败结束转
            MBProgressHUD.hide(for: view, animated: true)
            MBProgressHUD.showDelayHUDToView(view, message: "网络原因加载失败！")
        })
    }

}

